package warmachineroster.presentation.command;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;

import warmachineroster.Roster;
import warmachineroster.RosterElement;
import warmachineroster.UnitRosterElement;
import warmachineroster.WarcasterRosterElement;
import warmachinesystem.BaseUnit;
import warmachinesystem.Solo;

public class WarmachineRosterCommandFactory {

	public static final WarmachineRosterCommandFactory factoryInstance = new WarmachineRosterCommandFactory();

	public Command getCreateCommand(Object element, Roster roster) {
		if (element instanceof WarcasterRosterElement
				|| element instanceof UnitRosterElement) {
			RosterElementCreateCommand command = new RosterElementCreateCommand();
			command.setRosterElement((RosterElement) element);
			command.setRoster(roster);
			return command;
		} else if (element instanceof RosterElement) {
			BaseUnit bu = ((RosterElement) element).getUnitType();
			if (bu instanceof Solo) {
				RosterElementCreateCommand command = new RosterElementCreateCommand();
				command.setRosterElement((RosterElement) element);
				command.setRoster(roster);
				return command;
			}

		} else if (element instanceof List) {
			CompoundCommand ccommand = new CompoundCommand();
			for (Object elem : (List<?>) element) {
				ccommand.add(getCreateCommand(elem, roster));
			}
			return ccommand;
		}
		return UnexecutableCommand.INSTANCE;
	}

}
